﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Reflection;

namespace Kopytko
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            App.Current.ConnectionError += connectionError;
            App.Current.ConnectionOpened += connectionOpened;
            App.Current.InitMainProxy();
         
            overlayMessageBox.SetParent(grid_main);

            
        }

        void connectionOpened(object sender, System.EventArgs e)
        {
            this.Dispatcher.BeginInvoke(
                (System.Threading.ThreadStart)delegate()
                {
                    // ikonka...
                    connectionStatusIcon.Status = ConnectionStatusEnum.Connected;
                    showLoginWindow();
                });
        }


        #region Połączenie z WCF

        private void connectionError(object source, ConnectionErrorEventArgs e)
        {
            string txt;
            switch (e.Type)
            {
                case ConnectionType.MainService:
                    txt = "Wystąpił problem z połączeniem do głównego serwera";
                    break;
            }

            this.Dispatcher.BeginInvoke(
                (System.Threading.ThreadStart) delegate()
                                                    {
                                                        // ikonka...
                                                        connectionStatusIcon.Status = ConnectionStatusEnum.Failed;
                                                    });

        }

        #endregion

        

        void loadGames()
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            var uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            var _assembly_dir = System.IO.Path.GetDirectoryName(path);

            gamesList.Load(_assembly_dir);
        }

        private void gameList_gameChosen(object sender, GameChosenEventArgs e)
        {
            ChoosePlayersMessageBox chooseMbox = new ChoosePlayersMessageBox();

            overlayMessageBox.ShowDialog(chooseMbox);

            var playerType = chooseMbox.PlayerType;

            e.SelectedGame.Run(cc_main, playerType);
        }


        /// <summary>
        /// Jakas informacja o tym, ze jestesmy offline..
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StayOfflineCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            connectionStatusIcon.Status = ConnectionStatusEnum.Offline;
        }


        /// <summary>
        ///  TODO!
        /// </summary>
        private void RegisterCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Todo!");
        }

        private void Self_Loaded(object sender, RoutedEventArgs e)
        {
            loadGames();
        }

        private void LogInCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var param = e.Parameter as LogInCommandParameter;

            try
            {
                App.Current.SessionId = App.Current.ProxyMain.LogIn(param.Login, param.Pass);

                if (App.Current.SessionId.HasValue)
                {
                    this.Dispatcher.BeginInvoke(
                        (System.Threading.ThreadStart)delegate()
                        {
                            // ikonka...
                            connectionStatusIcon.Status = ConnectionStatusEnum.Logged;
                        });
                }
            }
            catch (Exception ex)
            {
                App.Current.NotifyConnectionError(this, ConnectionType.MainService, ex);

                showLoginWindow();
            }
        }


        void showLoginWindow()
        {
            overlayMessageBox.ShowDialog(new LoginMessageBox());
        }

    }
}
